package com.yiren.dbaa.module.practice_set.map.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.constant.DbConstant;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.db.dao.controller.TrajectDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.map.bean.LineBindBean;
import com.yiren.dbaa.module.practice_set.map.contract.MapManageContract;
import com.yiren.dbaa.module.practice_set.map.presenter.MapManagePresenter;
import com.yiren.dbaa.module.practice_set.site.view.dialog.SelectUserDialog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/29 9:34
 * @description: 地图管理
 */
public class MapManageActivity extends BaseActivity implements MapManageContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_map_name)
    TextView tvMapName;
    @BindView(R.id.tv_map_version)
    TextView tvMapVersion;
    @BindView(R.id.tv_map_version_bt)
    TextView tvMapVersionBt;
    @BindView(R.id.tv_lines)
    TextView tvLines;
    @BindView(R.id.tv_lines_bt)
    TextView tvLinesBt;
    @BindView(R.id.tv_using_range)
    TextView tvUsingRange;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;

    private Intent intent;
    private MapEntity map;

    private MapManageContract.Presenter mapManagePresenter;
    ActivityResultLauncher launcher;
    /*** 地图绑定的线路 */
    private List<LineInfoEntity> lines;

    /*** 线路选择状态 */
    private List<LineBindBean> lineBindBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_manage);
        ButterKnife.bind(this);
        initData();
        setData();
    }

    private void initData() {
        tvTitle.setText("地图管理");
        intent = getIntent();
        lineBindBeans = new ArrayList<>();
        map = (MapEntity) intent.getSerializableExtra("map");
        mapManagePresenter = new MapManagePresenter(this);
        //获取地图绑定的线路
        mapManagePresenter.getMapBindLines(map.getId(), this);
        //注册startActivityForResult
        launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    List<LineInfoEntity> lineInfos = (List<LineInfoEntity>) result.getData().getSerializableExtra("lines");
                    lineBindBeans.clear();
                    if (null == lineInfos || lineInfos.size() == 0) {
                        tvLines.setText("暂无线路");
                        tvLinesBt.setText("线路绑定");
                        if (lines == null || lines.size() == 0) {
                            //都没有选择线路，这里不处理
                        } else {
                            //原来选择的所有线路，取消了
                            for (LineInfoEntity lineInfo : lines) {
                                lineBindBeans.add(new LineBindBean(lineInfo, false));
                            }
                        }
                    } else {
                        String txt = "";
                        for (LineInfoEntity lineInfo : lineInfos) {
                            txt += lineInfo.getName() + "，";
                        }
                        tvLines.setText(txt.substring(0, txt.length() - 1));
                        tvLinesBt.setText("修改绑定线路");
                        if (lines == null || lines.size() == 0) {
                            //原来没有绑定线路，现在选择了
                            for (LineInfoEntity lineInfo : lineInfos) {
                                lineBindBeans.add(new LineBindBean(lineInfo, true));
                            }
                        } else {
                            //原来绑定了新路，现在也选择了
                            for (int i = 0; i < lines.size(); i++) {
                                for (int j = 0; j < lineInfos.size(); j++) {
                                    if (lines.get(i).getId().equals(lineInfos.get(j).getId())) {
                                        //原来绑定的线路和现在绑定的线路是同一条
                                        lineInfos.remove(j);
                                        break;
                                    }
                                }
                                //原来绑定的线路，现在没有绑定了
                                lineBindBeans.add(new LineBindBean(lines.get(i), false));
                            }
                            //这里得到了新绑定的线路
                            for (LineInfoEntity lineInfo : lineInfos) {
                                lineBindBeans.add(new LineBindBean(lineInfo, true));
                            }
                        }
                    }
                }
            }
        });
    }

    private void setData() {
        tvMapName.setText(map.getName());
        tvMapVersion.setText("V" + map.getMapVer() + "");
        if (map.getUseRange() == 1) {
            tvUsingRange.setText("个别用户使用");
        } else {
            tvUsingRange.setText("全部用户使用");
        }
        if (map.getMapVer() != 0) {
            tvMapVersionBt.setText("回退版本");
            tvMapVersionBt.setTextColor(getResources().getColor(R.color.white));
            tvMapVersionBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
            if (map.getUseRange() == 1) {
                tvUsingRangeBt.setClickable(true);
                tvUsingRangeBt.setText("修改范围");
                tvUsingRangeBt.setTextColor(getResources().getColor(R.color.white));
                tvUsingRangeBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
            } else {
                tvUsingRangeBt.setText("已是全部");
                tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
            }
        } else {
            tvUsingRange.setText("无用户使用");
            tvUsingRangeBt.setClickable(false);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_map_version_bt, R.id.tv_lines_bt, R.id.tv_using_range_bt, R.id.tv_release, R.id.tv_edit_map})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.tv_map_version_bt: //版本回退
                if (map.getMapVer() != 0) {
                    intent = new Intent(this, MapHistoryActivity.class);
                    intent.putExtra("map", map);
                    startActivity(intent);
                }
                break;
            case R.id.tv_lines_bt:  //绑定线路
                intent = new Intent(this, BindLineActivity.class);
                intent.putExtra("map", map);
                launcher.launch(intent);
                break;
            case R.id.tv_using_range_bt:  //修改使用范围
                if (map.getUseRange() == 1) {
                    List<String> user = new ArrayList<>();
                    user.add("全部用户使用");
                    user.add("个别用户使用");
                    SelectUserDialog.show(this, "修改范围", map.getUseRange() == 1 ? 1 : 0, user, new SelectUserDialog.OnSureClickListener() {
                        @Override
                        public void onSureClick(int selectIndex) {
                            mapManagePresenter.changeUserRange(MapManageActivity.this, map, selectIndex == 0 ? 2 : 1);
                        }
                    });
                }
                break;
            case R.id.tv_release:  //发布
                intent = new Intent(this, MapReleaseActivity.class);
                intent.putExtra("map", map);
                intent.putExtra("lines", (Serializable) lineBindBeans);
                startActivity(intent);
                break;
            case R.id.tv_edit_map:
                DbConstant.TRAJECT_DATA_URL = DbConstant.App_Main_Directory + LoadConfigApi.loadUserParamConfig(this).getUserInfo().getId() + "/map/" + map.getSiteCode() + "/" + DbConstant.TRAJECT_DATA_NAME;
                TrajectDbController.getInstance(true);
                startActivity(new Intent(this, MapCollectActivity.class));
                break;
        }
    }

    @Override
    public void updateUseRange(int range) {
        if (range == 2) {
            tvUsingRange.setText("全部用户使用");
            tvUsingRangeBt.setText("已是全部");
            tvUsingRangeBt.setBackground(null);
            tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
        } else {
            tvUsingRange.setText("个别用户使用");
        }
    }

    @Override
    public void setMapBindLines(List<LineInfoEntity> lines) {
        this.lines = lines;
        if (lines == null || lines.size() == 0) {
            tvLines.setText("暂无线路");
            tvLinesBt.setText("线路绑定");
        } else {
            String txt = "";
            for (LineInfoEntity lineInfo : lines) {
                txt += lineInfo.getName() + "，";
            }
            tvLines.setText(txt.substring(0, txt.length() - 1));
            tvLinesBt.setText("修改绑定线路");
        }
    }

}